package org.szgb.console.ht.meter.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.meter.entity.HtMeterReadingTask;
import org.szgb.console.ht.meter.mapper.HtMeterReadingTaskMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtMeterReadingTaskService {
    private final HtMeterReadingTaskMapper htMeterReadingTaskMapper;

    @Autowired
    public HtMeterReadingTaskService(HtMeterReadingTaskMapper htMeterReadingTaskMapper){
        this.htMeterReadingTaskMapper = htMeterReadingTaskMapper;
    }

    @Transactional(readOnly = true)
    public HtMeterReadingTask get(String id) {
        return htMeterReadingTaskMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtMeterReadingTask> findList(HtMeterReadingTask htMeterReadingTask) {
        if (htMeterReadingTask != null && htMeterReadingTask.getPageDto() != null) {
            Page<HtMeterReadingTask> page = new Page<>(htMeterReadingTask.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htMeterReadingTask.setPage(page);
        }
        return htMeterReadingTaskMapper.findList(htMeterReadingTask);
    }

    @Transactional(readOnly = true)
    public List<HtMeterReadingTask> findListTodayNoDeal(HtMeterReadingTask htMeterReadingTask) {
        return htMeterReadingTaskMapper.findListTodayNoDeal(htMeterReadingTask);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtMeterReadingTask htMeterReadingTask) {
        return htMeterReadingTaskMapper.getCount(htMeterReadingTask);
    }

    @Transactional()
    public void save(HtMeterReadingTask htMeterReadingTask) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htMeterReadingTask.getId() == null || htMeterReadingTask.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htMeterReadingTask.setCreatedBy(htMeterReadingTask.getUpdatedBy());
            // 添加UUID
            htMeterReadingTask.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htMeterReadingTaskMapper.insert(htMeterReadingTask);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htMeterReadingTaskMapper.update(htMeterReadingTask);
        }
    }

    @Transactional
    public void delete(String ids) {
        htMeterReadingTaskMapper.delete(ids.split(","));
    }
}
